layui.use(['table', 'jquery','form', 'layer','laydate'], function () {
    let table = layui.table;
    let $ = layui.$;
    let form = layui.form;
    let layer = layui.layer;
    let laydate = layui.laydate;


    //日期范围
    laydate.render({
        elem: '#timeTest'
        //设置开始日期、日期日期的 input 选择器
        //数组格式为 2.6.6 开始新增，之前版本直接配置 true 或任意分割字符即可
        , range: ['#limitStartTime', '#limitEndTime']
        , format: 'yyyy-MM-dd HH:mm:ss'
    });

    // 试卷类型下拉列表框监视
    form.on('select(paperType)', function (data) {
        //console.log(data.value); 得到被选中的值                
        if (data.value == 2) {
            $("#date").show();
        } else {
            $("#date").hide();
        }
        form.render();
    });

    // 年级名称下拉列表框监视
    form.on('select(levelName)', function (data) {
        //console.log(data.value); 得到被选中的值     
        sessionStorage.setItem("addQuestionLevelName", data.value);

        $("#subjectName option").each(function (i) {
            if (i != 0) {
                $(this).remove();
            }
        })
        form.render();
        if (data.value == "") {
            reture;
        }

        $.ajax({
            url: 'http://localhost:8080/subject/selectByMore',
            type: 'get',
            data: { levelName: data.value },
            success: function (vo) {
                if (vo.code == 200) {
                    $.each(vo.data, function (i, s) {
                        $("#subjectName").append("<option value='" + s.subjectName + "'>" + s.subjectName + "</option>");
                    });
                    form.render();
                }
            }
        });
    });

    // 学科名称下拉列表框监视
    form.on('select(subjectName)', function (data) {
        sessionStorage.setItem("addQuestionSubjectName", data.value);
    });


    // 解决表格在排序之后没有重新渲染问题
    table.on('sort(paperQuestionList)', function (obj) {
        table.reload('paperQuestionTable', {
            initSort: obj
        });
    });
    table.reload('paperQuestionTable', {});


    //头工具栏事件
    table.on('toolbar(paperQuestionList)', function (obj) {
        var checkStatus = table.checkStatus('paperQuestionTable');
        var data = checkStatus.data;

        switch (obj.event) {
            case 'search':
                //重载表格加载新的数据, 引用render的id
                table.reload('paperQuestionTable', {
                    method: 'get',
                    where: {
                        difficult: $('#difficult').val(),
                        subjectName: sessionStorage.getItem("addQuestionSubjectName"),
                        levelName: sessionStorage.getItem("addQuestionLevelName"),
                        questionType:sessionStorage.getItem("addQuestionQuestionType"),
                    },
                    page: {
                        curr: 1  //查询回来后默认显示在第一页
                    }
                });
                break;
            case 'getCheckData':
                layer.alert(JSON.stringify(data));
                break;
            case 'getCheckLength':
                layer.msg('选中了：' + data.length + ' 个');
                break;
            case 'addAll':
                let did = sessionStorage.getItem("did");
                
                $.each(data,function(index,p){
                    app.paperQuestionIndex=app.paperQuestionIndex+1;
                    // $("#delQuestionBtn").show();

                    if(p.questionType==1){
                        $("#"+did+"").append(
                            "<div class='layui-input-block' id='qs"+app.paperQuestionIndex+"' style='width:90%;padding: 30px;margin-top: 10px;-webkit-box-shadow: 0 2px 12px 0 rgb(0 0 0 / 30%);box-shadow: 0 2px 12px 0 rgb(0 0 0 / 30%);position: relative;'>"+
                                "<a style='position: absolute;top: 10px;right: 20px;font-size: 10px;color: #1890ff;cursor: pointer;' onClick='delQuestion("+app.paperQuestionIndex+")'>删除</a>"+
                                "<input type='hidden' value='"+p.paperQuestionId+"' class='hiddenPaperQuestionId'>"+
                                "<input type='hidden' value="+p.score+" class='hiddenScore'></input>"+
                                "<div>"+
                                    "<span id='title2'>"+p.questionContentInfo.title+"</span>"+
                                    // "<span id='title2'>题目"+app.paperQuestionIndex+"："+p.questionContentInfo.title+"</span>"+
                                "</div>"+
                                "<div style='margin-top: 30px;'>"+
                                    "<div style='margin-left: 30px;display: inline-block;'>"+
                                        "A&nbsp;&nbsp;&nbsp;<span id='questionA2'>"+p.questionContentInfo.questionA+"</span>"+
                                    "</div>"+
                                    "<div style='margin-left: 30px;display: inline-block;'>"+
                                        "B&nbsp;&nbsp;&nbsp;<span id='questionB2'>"+p.questionContentInfo.questionB+"</span>"+
                                    "</div>"+
                                    "<div style='margin-left: 30px;display: inline-block;'>"+
                                        "C&nbsp;&nbsp;&nbsp;<span id='questionA2'>"+p.questionContentInfo.questionC+"</span>"+
                                    "</div>"+
                                    "<div style='margin-left: 30px;display: inline-block;'>"+
                                        "D&nbsp;&nbsp;&nbsp;<span id='questionB2'>"+p.questionContentInfo.questionD+"</span>"+
                                    "</div>"+
                                "</div>"+
                            "</div>"
                        );
                    }else if(p.questionType==2){
                        $("#"+did+"").append(
                            "<div class='layui-input-block' id='qs"+app.paperQuestionIndex+"' style='width:90%;padding: 30px;margin-top: 10px;-webkit-box-shadow: 0 2px 12px 0 rgb(0 0 0 / 30%);box-shadow: 0 2px 12px 0 rgb(0 0 0 / 30%);position: relative;'>"+
                                "<a style='position: absolute;top: 10px;right: 20px;font-size: 10px;color: #1890ff;cursor: pointer;' onClick='delQuestion("+app.paperQuestionIndex+")'>删除</a>"+
                                "<input type='hidden' value='"+p.paperQuestionId+"' class='hiddenPaperQuestionId'>"+
                                "<input type='hidden' value="+p.score+" class='hiddenScore'></input>"+
                                "<div>"+
                                    "<span id='title2'>"+p.questionContentInfo.title+"</span>"+
                                "</div>"+
                                "<div style='margin-top: 30px;'>"+
                                    "<div style='margin-left: 30px;display: inline-block;'>"+
                                        "A&nbsp;&nbsp;&nbsp;<span id='questionA2'>"+p.questionContentInfo.questionA+"</span>"+
                                    "</div>"+
                                    "<div style='margin-left: 30px;display: inline-block;'>"+
                                        "B&nbsp;&nbsp;&nbsp;<span id='questionB2'>"+p.questionContentInfo.questionB+"</span>"+
                                    "</div>"+
                                    "<div style='margin-left: 30px;display: inline-block;'>"+
                                        "C&nbsp;&nbsp;&nbsp;<span id='questionA2'>"+p.questionContentInfo.questionC+"</span>"+
                                    "</div>"+
                                    "<div style='margin-left: 30px;display: inline-block;'>"+
                                        "D&nbsp;&nbsp;&nbsp;<span id='questionB2'>"+p.questionContentInfo.questionD+"</span>"+
                                    "</div>"+
                                    "<div style='margin-left: 30px;display: inline-block;'>"+
                                        "E&nbsp;&nbsp;&nbsp;<span id='questionB2'>"+p.questionContentInfo.questionE+"</span>"+
                                    "</div>"+
                                "</div>"+
                            "</div>"
                        );
                    }else if(p.questionType==3){
                        $("#"+did+"").append(
                            "<div class='layui-input-block' id='qs"+app.paperQuestionIndex+"' style='width:90%;padding: 30px;margin-top: 10px;-webkit-box-shadow: 0 2px 12px 0 rgb(0 0 0 / 30%);box-shadow: 0 2px 12px 0 rgb(0 0 0 / 30%);position: relative;'>"+
                                "<a style='position: absolute;top: 10px;right: 20px;font-size: 10px;color: #1890ff;cursor: pointer;' onClick='delQuestion("+app.paperQuestionIndex+")'>删除</a>"+
                                "<input type='hidden' value='"+p.paperQuestionId+"' class='hiddenPaperQuestionId'>"+
                                "<input type='hidden' value="+p.score+" class='hiddenScore'></input>"+
                                "<div>"+
                                    "<span id='title2'>"+p.questionContentInfo.title+"</span>"+
                                "</div>"+
                                "<div style='margin-top: 30px;'>"+
                                    "<div style='margin-left: 30px;display: inline-block;'>"+
                                        "A&nbsp;&nbsp;&nbsp;<span id='questionA2'>"+p.questionContentInfo.questionA+"</span>"+
                                    "</div>"+
                                    "<div style='margin-left: 30px;display: inline-block;'>"+
                                        "B&nbsp;&nbsp;&nbsp;<span id='questionB2'>"+p.questionContentInfo.questionB+"</span>"+
                                    "</div>"+
                                "</div>"+
                            "</div>"
                        );
                    }
                    
                });
     
                layer.closeAll();
                break;

            //自定义头工具栏右侧图标 - 提示
            case 'LAYTABLE_TIPS':
                layer.alert('这是工具栏右侧自定义的一个图标按钮');
                break;
        };
    });



    // 监听行工具事件
    table.on('tool(paperQuestionList)', function (obj) {
        //获取按钮对象lay-event属性值
        let event = obj.event;

        switch (event) {
            //在不同的case中做不同的按钮事件处理
            case 'view':
                $(".title2").text(obj.data.questionContentInfo.title);
                $(".questionA2").text(obj.data.questionContentInfo.questionA);
                $(".questionB2").text(obj.data.questionContentInfo.questionB);
                $(".questionC2").text(obj.data.questionContentInfo.questionC);
                $(".questionD2").text(obj.data.questionContentInfo.questionD);
                $(".questionE2").text(obj.data.questionContentInfo.questionE);
                if (obj.data.questionType == 1) {
                    layer.open({
                        type: 1,
                        title: ['预览', 'font-size:16px'],
                        anim: 5,   // 动画
                        offset: '50px',  // 坐标
                        resize: false,  // 是否允许拉伸改变宽高
                        area: ['auto'], // 设置宽，高
                        shadeClose: true, //点击遮罩关闭
                        scrollbar: false,   // 屏蔽浏览器滚动（false: 则屏蔽）
                        content: $("#showOne")
                    });
                } else if (obj.data.questionType == 2) {
                    layer.open({
                        type: 1,
                        title: ['预览', 'font-size:16px'],
                        anim: 5,   // 动画
                        offset: '50px',  // 坐标
                        resize: false,  // 是否允许拉伸改变宽高
                        area: ['auto'], // 设置宽，高
                        shadeClose: true, //点击遮罩关闭
                        scrollbar: false,   // 屏蔽浏览器滚动（false: 则屏蔽）
                        content: $("#showTwo")
                    });
                } else if (obj.data.questionType == 3) {
                    layer.open({
                        type: 1,
                        title: ['预览', 'font-size:16px'],
                        anim: 5,   // 动画
                        offset: '50px',  // 坐标
                        resize: false,  // 是否允许拉伸改变宽高
                        area: ['auto'], // 设置宽，高
                        shadeClose: true, //点击遮罩关闭
                        scrollbar: false,   // 屏蔽浏览器滚动（false: 则屏蔽）
                        content: $("#showThree")
                    });
                }
                break;
        }
    });


});